#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygame
pygame.init()
from sys import exit
from pygame.locals import *
from gameobjects.vector2 import Vector2
from math import *




class World(object):
	x = 100
	y = 100

	
class Zombie(World):
	def __init__(self,name):
		self.name = name
		self.health = 20
		
class Player(object):
	def __init__(self,name):
		self.name = name
		self.x = 400
		self.y = 300
		self.position = (400,300)
	
	def movement(self,enemy,bullets):
		pressed_keys = pygame.key.get_pressed()
		if pressed_keys[K_s]:
			for count in range(0,len(enemy)):
				enemy[count].y -= 1
			for count in range(0,len(bullets)):
				x,y = bullets[count].position
				y -= 1
				bullets[count].position = (x,y)
				
		pressed_keys = pygame.key.get_pressed()
		if pressed_keys[K_w]:
			for count in range(0,len(enemy)):
				enemy[count].y += 1
			for count in range(0,len(bullets)):
				x,y = bullets[count].position
				y += 1
				bullets[count].position = (x,y)
				
		pressed_keys = pygame.key.get_pressed()
		if pressed_keys[K_a]:
			for count in range(0,len(enemy)):
				enemy[count].x += 1
			for count in range(0,len(bullets)):
				x,y = bullets[count].position
				x += 1
				bullets[count].position = (x,y)
				
		pressed_keys = pygame.key.get_pressed()
		if pressed_keys[K_d]:
			for count in range(0,len(enemy)):
				enemy[count].x -= 1
			for count in range(0,len(bullets)):
				x,y = bullets[count].position
				x -= 1
				bullets[count].position = (x,y)
		
				
	def shooting(self,event,bullets):
		if (event.type == MOUSEBUTTONDOWN):
			
			
			bullets.append(Bullet("bullet"))
			bullets[len(bullets)-1].position = (self.x,self.y)
			mouse_pos = pygame.mouse.get_pos()
			direction = Vector2.from_points((self.x,self.y), mouse_pos)
			direction.normalize()
			
			angle = degrees(atan2(direction.x,direction.y))
			if angle < 0:
				angle = 360 + angle
			print angle
			bullets[len(bullets)-1].angle = angle
			
			#print bullets[0].position
			#print bullets[0].angle
			
class Bullet(World):
	def __init__(self,name):
		self.name = name
		self.angle = 0
		self.speed = 3
		self.position = (0,0)
		
	def animate(self,bullets,player):
		if len(bullets) > 0:
			count = 0
			while count < len(bullets):
				distance = Vector2(bullets[count].position,player.position)
				if distance.get_magnitude() > 800:
					del bullets[count]
				else:
					direction = Vector2(0, 0)
					direction.x = sin(bullets[count].angle*pi/180.0)
					direction.y = cos(bullets[count].angle*pi/180.0)
					direction.normalize()
					bullets[count].position += direction * self.speed

				if count >= len(bullets)-1:
					break
				count += 0
				
				
